package graph_interface;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import sun.net.RegisteredDomain;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;

import java.io.*;

public class login extends Application{
    private final Label lab1=new Label("用户名：");
    private final Label lab2=new Label("密码：");
    private final TextField tf=new TextField();
    private final PasswordField pwf=new PasswordField();
    private final Button bt1=new Button("登录");
    private final Button bt2=new Button("修改密码");
    private final Label txt=new Label("注册");
    @Override
    public void start(Stage stage)
    {
        GridPane rootPane=new GridPane();
        rootPane.add(lab1,0,0);
        rootPane.add(tf,1,0);
        rootPane.add(lab2,0,1);
        rootPane.add(pwf,1,1);
        rootPane.add(bt1,0,2);
        rootPane.add(bt2,1,2);
        rootPane.add(txt, 2,2);
        
        lab1.setTextFill(Color.RED);
        lab2.setTextFill(Color.RED);
        bt2.setPrefWidth(80);
        bt1.setStyle("-fx-background-color: green;-fx-text-fill: white;");
        bt2.setStyle("-fx-background-color: green;-fx-text-fill: white;");
        bt1.setOnAction(e->
        {
        	try(FileReader fr=new FileReader("database/user.txt");BufferedReader br=new BufferedReader(fr,30))
                {
                    String str=null;
                    while((str=br.readLine())!=null)
                    {
                        boolean isAdm=false;
                        String[] sarr=str.split(" ");
                        if(sarr.length==3)
                        {
                            isAdm=true;
                        }
                        if(sarr[0].equals(tf.getText())&&sarr[1].equals(pwf.getText()))
                        {
                            System.out.println("密码正确，登录成功！！！");
//                            stage.close();
                            if(isAdm)
                            {
                            	TeaInsert ti=new TeaInsert();
                            	ti.setName(sarr[0]);
                            	ti.start(new Stage());
                            }
                            else
                            {
                            	StuQuery sq=new StuQuery();
                            	sq.setName(sarr[0]);
                            	sq.start(new Stage());
                            	
                            }
                            
                            return;
                        }
                    }
                    System.out.println("密码或用户名错误，请重试");
                    pwf.setStyle("-fx-border-color: red");
                    pwf.setText("");
                }
                catch(FileNotFoundException ex)
                {
                    System.out.println("文件未找到....");
                }
                catch(IOException ex)
                {
                    System.out.println("ERROR");
                }
        });
        txt.setTextFill(Color.BLUE);
        txt.setUnderline(true);
        txt.setFocusTraversable(true);
        txt.setOnMouseClicked(event->
        {
        	try {
				new Registered().start(new Stage());
			} 
        	catch (Exception e) {
				e.printStackTrace();
			}
        });
        rootPane.setPadding(new Insets(30));
        rootPane.setHgap(20);
        rootPane.setVgap(20);
        rootPane.setStyle("-fx-background-image: url('img/jiesu.jpg')");
        rootPane.setAlignment(Pos.CENTER);
        Scene scene=new Scene(rootPane,400,300);
        stage.setTitle("运动会计分管理系统");
        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args)
    {
    	Application.launch(args);
    }
}
